package com.yenroc.ho.context;

import com.yenroc.ho.resources.GlobalConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author： heyanpeng
 * @date： 2021/7/29
 */
@Component
public class GlobalContext {

    private final AttachmentCache attachmentCache;

    @Autowired
    public GlobalContext(AttachmentCache attachmentCache) {
        this.attachmentCache = attachmentCache;
    }


    public GlobalConnection getConnection(String groupId) throws Exception {
        if (attachmentCache.containsKey(groupId, GlobalConnection.class.getName())) {
            return attachmentCache.attachment(groupId, GlobalConnection.class.getName());
        }
        throw new RuntimeException("non exists connection");
    }

    public void setConnection(String groupId, GlobalConnection globalConnection) {
        attachmentCache.attach(groupId, GlobalConnection.class.getName(), globalConnection);
    }
}
